/*
 * Copyright 2011-2016 YueJi.com All right reserved. This software is the
 * confidential and proprietary information of YueJi.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with YueJi.com.
 */
package com.bops.dal.user.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.bops.dal.user.dao.RoleDao;
import com.bops.dal.user.dataobject.RoleDO;
import com.bops.dal.user.queryobject.RoleQuery;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.yue.commons.db.ibatis.YueBaseSqlMapClientDaoSupport;

/**
 */
public class RoleDaoImpl extends YueBaseSqlMapClientDaoSupport implements RoleDao {
    
    @SuppressWarnings("unchecked")
    public List<RoleDO> listRole(RoleQuery query) {
        if (query==null)
        {
            query = new RoleQuery();
        }
        return getSqlMapClientTemplate().queryForList("BOPS_ROLE.list",query);
    }

    /**
     * 批量更新用户权限
     * @param addList
     * @param deleteList
     */
    public void batchUpdateRole(final List<RoleDO> addList,final List<RoleDO> deleteList)
    {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public Object doInSqlMapClient(SqlMapExecutor executor)
                            throws SQLException {
                long t1 = System.currentTimeMillis();
                // 开启事物
                getSqlMapClient().startTransaction();
                // 开始插入批处理
                executor.startBatch();
                //添加新的权限
                if (addList!=null)
                {
                    for (RoleDO record:addList)
                    {
                        executor.insert("BOPS_ROLE.insert",record);
                    }
                }
                
                //删除权限
                if(deleteList!=null)
                {
                    for (RoleDO deleteTmpDO : deleteList) {
                        if (deleteTmpDO.getUsersId()!=null && deleteTmpDO.getRoleName()!=null)
                        {
                            Map map = new HashMap();
                            List<String> list = new ArrayList<String>();
                            list.add(""+deleteTmpDO.getUsersId());
                            map.put("roleName",deleteTmpDO.getRoleName());
                            map.put("usersIdList",list);  
                            executor.insert("BOPS_ROLE.delete",map);
                        }
                    }
                }
                
                // 执行批处理
                executor.executeBatch();
                // 执行事物
                getSqlMapClient().commitTransaction();
                // 结束事物
                getSqlMapClient().endTransaction();

                long t2 = System.currentTimeMillis();
                // 日志记录批处理结束
                logger.info("本次批处理处理时间=" +(t2-t1)+ "ms");
                // 返回处理条数
                return 0;
            }
        });
    }
}
